$kk=`ls /data4/bsi/refdata/genetics/1000Genomes/downloaded_data/release/20110521/impute/ALL_1000G_phase1integrated_v3_impute/*legend*`;
@kk=split("\n",$kk);
#open(WRBUFF,">HIS_MAF_0.05");
if(@ARGV != 3)
{
	die " perl perl_filter_chn_maf.pl <FIELD NUM IN THE LEGEND FILE> <OUTFILE NAME> <MAF required>\n";
}
chomp($ARGV[0]);
chomp($ARGV[1]);
chomp($ARGV[2]);
$outfil=$ARGV[1];
$field = $ARGV[0] -1;
$maf=$ARGV[2];
open(WRBUFF,">$outfil") or die "not able to write the file $outfil\n";

#print $kk[0]."\n";
for($i=0;$i<@kk;$i++)
{
	
	open(BUFF,"gunzip -c $kk[$i] |");
	$n=0;
	$line=<BUFF>;
	while(<BUFF>)
	{
		chomp($_);
		@a=split(" ",$_);
		if($field > @a)
		{
			die "field $field > total size array  @a\n";
		}
		if($a[$field]>$maf && length($a[2]) ==1 && length($a[3])==1)
		{
			print WRBUFF $a[0]."\t".$a[1]."\t".$a[10]."\n";
			$n++;
		}
	}
	print "dealing with $kk[$i] $n\n";
}
